<?php

class Application_Model_UserMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_User');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_User $user)
	{
		$data = array(
				'id' => $user->getId(),
				'mobile_tel' => $user->getMobileTel(),
				'email'   => $user->getEmail(),
				'name' => $user->getName(),
				'password' => $user->getPassword(),
				'created' => date('Y-m-d H:i:s'),
		);
	
		if (null === ($id = $user->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function find($id, Application_Model_User $user)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$user->setId($row->id)
		->setEmail($row->email)
		->setName($row->name)
		//->setPassword($row->password)
		->setCreated($row->created);
	}
	
	public function findByEmail($email){
		$entries   = array();
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('email = ?', $email);
		$resultSet = $table->fetchAll($where);
		
		foreach ($resultSet as $row) {
			$subscribe = new Application_Model_User();
			$subscribe->setId($row->id)
			->setEmail($row->email)
			->setName($row->name)
			//->setPassword($row->password)
			->setCreated($row->created);
			;
			$entries[] = $subscribe;
		}
		
		return $entries;
	}
	
	public function findByMobile($mobile){
		$entries   = array();
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('mobile_tel = ?', $mobile);
		$resultSet = $table->fetchAll($where);
		
		foreach ($resultSet as $row) {
			$subscribe = new Application_Model_User();
			$subscribe->setId($row->id)
			->setMobileTel($row->mobile_tel)
			->setName($row->name)
			->setPassword($row->password)
			->setCreated($row->created);
			;
			$entries[] = $subscribe;
		}
		
		return $entries;
	}
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_User();
			$entry->setId($row->id)
			->setEmail($row->email)
			->setMobileTel($row->mobile_tel)
			->setHomeTel($row->home_tel)
			->setUserGroup($row->user_group)
			->setName($row->name)
			->setPassword($row->password)
			->setCreated($row->created);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function validateLogin($username,$password){
		//$table = $this->getDbTable();
		//$db = $table->getAdapter();
		$db = Zend_Db_Table::getDefaultAdapter();
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName('user')
		->setIdentityColumn('email')
    			->setCredentialColumn('password')
    			->setIdentity($username)//认证的值
    			->setCredential($password);
		
		$auth = Zend_Auth::getInstance();
		//$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		$result = $auth->authenticate($authAdapter);//do validation
		
		//$row = $authAdapter->getResultRowObject();
		//$userObject = new Application_Model_User();
		
		//$userObject->setId($row->id)
		//	->setEmail($row->email)
		//	->setName($row->name)
			//->setPassword($row->password)
		//	->setCreated($row->created);
		
		
		
		//$entries   = array();
		//$entries['result'] = $result;
		//$entries['user'] = $userObject;
		
		return $result;
	}
	
	public function validateMobileLogin($mobile,$password){
		$db = Zend_Db_Table::getDefaultAdapter();
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName('user')
		->setIdentityColumn('mobile_tel')
		->setCredentialColumn('password')
		->setIdentity($mobile)//认证的值
		->setCredential($password);
		
		$auth = Zend_Auth::getInstance();
		//$auth->setStorage(new Zend_Auth_Storage_Session('user'));
		$result = $auth->authenticate($authAdapter);//do validation
		
		return $result;
	}
	
	public function hasMobile($mobile){
		//$entries   = array();
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('mobile_tel = ?', $mobile);
		$resultSet = $table->fetchAll($where);
		
		if(count($resultSet) == 0 || $resultSet === null){
			return false;
		}
		return true;
	}
	
	public function register(Application_Model_User $user){
		$data = array(
				'mobile_tel' => $user->getMobileTel(),
				'email'   => $user->getEmail(),
				'name' => $user->getName(),
				'password' => $user->getPassword(),
				'created' => date('Y-m-d H:i:s'),
		);
		
		
		$this->getDbTable()->insert($data);
		
	}

}

